package org.zhadoop.stream.cql.semanticanalyzer.parser.context;

import java.util.List;

import org.zhadoop.stream.cql.DriverContext;
import org.zhadoop.stream.cql.exception.CQLException;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.hooks.SemanticAnalyzeHook;
import org.zhadoop.stream.cql.semanticanalyzer.SemanticAnalyzer;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextwalker.ParseContextWalker;
import org.zhadoop.stream.cql.tasks.Task;

/**
 * 解析内容的抽象类，用来注册一些解析类的公共方法
 * 
 * 为了避免toString方法的遗忘，所以将本例从接口改为抽象类
 * 如果是接口的话，不写toString方法，也不会导致报错
 * 
 * @author zwd
 *
 */
public abstract class ParseContext {
	
	public abstract String toString();
	
	/**
	 * 创建对应语句执行的task
	 * @param driverContext
	 * @param analyzeHooks
	 * @return
	 * @throws CQLException
	 */
	public abstract Task createTask(DriverContext driverContext,List<SemanticAnalyzeHook> analyzeHooks) throws CQLException;
	
	/**
	 * 创建语义分析执行解析器
	 * @return
	 * @throws SemanticAnalyzerException
	 */
	public abstract SemanticAnalyzer createAnalyzer() throws SemanticAnalyzerException;
	
	
	  /**
     * 遍历自身以及子表达式
     */
    public void walk(ParseContextWalker walker)
    {
        if (!walker.walk(this))
        {
            walkChild(walker);
        }
    }
    
    /**
     * 遍历表达式
     * @param walker
     * @param parseContext
     */
    protected void walkExpression(ParseContextWalker walker,ParseContext parseContext){
    	if(parseContext == null){
    		return;
    	}
    	if(! walker.walk(parseContext)){
    		parseContext.walk(walker);
    	}
    }
    
    /**
     * 遍历表达式列表
     */
    protected void walkExpressions(ParseContextWalker walker, List<BaseExpressionParseContext> expressions)
    {
        for (BaseExpressionParseContext child : expressions)
        {
            walkExpression(walker, child);
        }
    }
    

	protected abstract void walkChild(ParseContextWalker walker);
}
